package cn.stylefeng.guns.modular.business.frequency.financesettlement.controller;

import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.finance.model.result.PkgFinanceSettlement;
import cn.stylefeng.guns.modular.business.frequency.financesettlement.entity.FinanceSettlement;
import cn.stylefeng.guns.modular.business.frequency.financesettlement.model.param.FinanceSettlementDTO;
import cn.stylefeng.guns.modular.business.frequency.financesettlement.service.FinanceSettlementService;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: 财务结算记录controller
 * @Author jzy
 * @Date 2021/3/15 0015
 **/
@Api(tags = "日记账模块")
@RestController
@RequestMapping("/api/settlement")
public class FinanceSettlementController {

    @Resource
    private FinanceSettlementService financeSettlementService;

    @ApiOperation(value = "结算记录列表")
    @GetMapping
    public ResponseData getFinanceSettlementList( FinanceSettlementDTO financeSettlementDTO) {
        // 排序
        if (StringUtils.isNotBlank(financeSettlementDTO.getColumn()) ) {
            financeSettlementDTO.setColumn("fs.settlement_time");
        } else {
            financeSettlementDTO.setColumn("fs.settlement_time");
            financeSettlementDTO.setAsc(false);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getBeginSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getBeginSearchTime().replace(" ",""))){
            financeSettlementDTO.setBeginSearchTime(null);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getEndSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getEndSearchTime().replace(" ",""))){
            financeSettlementDTO.setEndSearchTime(null);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getOperatorBeginSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getOperatorBeginSearchTime().replace(" ",""))){
            financeSettlementDTO.setOperatorBeginSearchTime(null);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getOperatorEndSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getOperatorEndSearchTime().replace(" ",""))){
            financeSettlementDTO.setOperatorEndSearchTime(null);
        }

        return financeSettlementService.getFinanceSettlementList(PageFactory.defaultPage(), financeSettlementDTO);
    }

    @ApiOperation(value = "结算记录详情")
    @GetMapping("/{id}")
    public ResponseData getFinanceSettlementDetail(@PathVariable String id) {
        PkgFinanceSettlement financeSettlement = financeSettlementService.getFinanceSettlementDetail(id);
        return ResponseData.andaSuccess(financeSettlement);
    }

    @ApiOperation("日记账新增")
    @PostMapping
    public ResponseData addFinanceSettlement(@RequestBody FinanceSettlement financeSettlement) {
        StringBuilder errorMsg = new StringBuilder();
        if (!financeSettlementService.checkFinanceSettlementAdd(financeSettlement, errorMsg)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        financeSettlementService.addFinanceSettlement(financeSettlement);
        return ResponseData.andaSuccess();
    }

    @ApiOperation("日记账新增")
    @DeleteMapping("/{id}")
    public ResponseData delete(@PathVariable Long id) {
        financeSettlementService.removeById(id);
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "日记账导出")
    @GetMapping("/download")
    public void exportExcel(FinanceSettlementDTO financeSettlementDTO, HttpServletResponse response) {
        // 排序
        if (StringUtils.isNotBlank(financeSettlementDTO.getColumn()) ) {
            financeSettlementDTO.setColumn("fs.settlement_time");
        } else {
            financeSettlementDTO.setColumn("fs.settlement_time");
            financeSettlementDTO.setAsc(false);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getBeginSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getBeginSearchTime().replace(" ",""))){
            financeSettlementDTO.setBeginSearchTime(null);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getEndSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getEndSearchTime().replace(" ",""))){
            financeSettlementDTO.setEndSearchTime(null);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getOperatorBeginSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getOperatorBeginSearchTime().replace(" ",""))){
            financeSettlementDTO.setOperatorBeginSearchTime(null);
        }
        if(StringUtils.isNotBlank(financeSettlementDTO.getOperatorEndSearchTime())&&"Invaliddate".equals(financeSettlementDTO.getOperatorEndSearchTime().replace(" ",""))){
            financeSettlementDTO.setOperatorEndSearchTime(null);
        }

        financeSettlementService.exportExcel(financeSettlementDTO, response);
    }
}
